﻿using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace SystemWnioskujacy
{
    public partial class MainForm : Form
    {
        public ConsoleForm ConsoleChildForm = new ConsoleForm();

        public bool exitFromConsole = false;
        
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            ConsoleChildForm.MdiParent = this;
            ConsoleChildForm.Show();
        }

        private void FileFormAdd(string path)
        {
            FileForm newChild = new FileForm(path);
            newChild.MdiParent = this;
            newChild.Text = newChild.path;
            newChild.Show();
        }

        private void FileFormAdd()
        {
            FileForm newChild = new FileForm();
            newChild.MdiParent = this;
            newChild.Text = newChild.path;
            newChild.Show();
        }

        private void SaveActive(string path)
        {
            FileForm activeChild = (FileForm)this.ActiveMdiChild;
            activeChild.path = path;
            RichTextBox content = (RichTextBox)activeChild.Controls["FileBox"];

            System.IO.File.WriteAllText(activeChild.path, content.Text);
            zapiszToolStripMenuItem.Enabled = true;
            activeChild.Text = path;
        }

        private void SaveActive()
        {
            FileForm activeChild = (FileForm)this.ActiveMdiChild;
            RichTextBox content = (RichTextBox)activeChild.Controls["FileBox"];

            System.IO.File.WriteAllText(activeChild.path, content.Text);
            activeChild.Text = activeChild.path;
        }

        private void nowyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FileFormAdd();
        }

        private void zapiszToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveActive();
        }

        private void zapiszJakoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFile.ShowDialog();
        }

        private void wczytajToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadFile.ShowDialog();
        }

        private void wczytajToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            FileForm activeChild = (FileForm)this.ActiveMdiChild;
            RichTextBox content = (RichTextBox)activeChild.Controls["FileBox"];

            // TODO: Parser
            Parser.Parser.Parse(content.Text);
        }

        private void LoadFile_FileOk(object sender, CancelEventArgs e)
        {
            FileFormAdd(LoadFile.FileName);
        }

        private void SaveFile_FileOk(object sender, CancelEventArgs e)
        {
            SaveActive(SaveFile.FileName);
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
            if (!exitFromConsole) {
                bool res = Function.Quit(false);
                if (!res) {
                    e.Cancel = true;
                }
            }
        }
    }
}
